﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DanhMucChucVu : DBInteractionBase
    {
        public string TaiKhoan { get; set; }
        public string MaChucVu { get; set; }
        public string TenChucVu { get; set; }

        public bool Insert(DanhMucChucVu obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucChucVu_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", ConvertNvarcharToVarchar(obj.MaChucVu)));
                    dbcmd.Parameters.Add(new SqlParameter("@TenChucVu", obj.TenChucVu));
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", TaiKhoan));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string id, string taikhoan)
        {
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucChucVu_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", id));
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", taikhoan));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucChucVu_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DanhMucChucVu_SelectById: " + ex.Message);
            }
        }

        public DataTable SelectAll(DanhMucChucVu obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucChucVu_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu",
                        string.IsNullOrEmpty(obj.MaChucVu) ? "" : obj.MaChucVu));
                    dbcmd.Parameters.Add(new SqlParameter("@TenChucVu",
                        string.IsNullOrEmpty(obj.TenChucVu) ? "" : obj.TenChucVu));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DanhMucChucVu_SelectAll: " + ex.Message);
            }
        }
    }
}